import 'dart:async';

import 'package:flutter/material.dart';
import 'package:xin_he/utils/xh_styles.dart';

class XinHeTimerCountDownWidget extends StatefulWidget {
  //开始计时回调事件
  final Function? onStarEvent;
  //周期回调事件
  final Function? onPeriodic;
  //计时完成回调
  final Function? onTimerFinish;
  //周期值
  final int? periodicSec;
  //倒计时多久/s
  final int? maxSec;

  //主体颜色
  final Color? themeColor;

  //可点击状态下显示的文本
  final String? norText;
  //可点击状态下显示的文本样式
  final TextStyle? norTextStyle;

  XinHeTimerCountDownWidget({
    this.onStarEvent,
    this.onPeriodic,
    this.onTimerFinish,
    this.periodicSec,
    this.maxSec,
    this.norText,
    this.norTextStyle,
    this.themeColor
  });

  @override
  State<StatefulWidget> createState() => XinHeTimerCountDownWidgetState();
}

class XinHeTimerCountDownWidgetState extends State<XinHeTimerCountDownWidget> {
  //计时器
  Timer? _timer;
  //当前秒数
  int _countdownTime = 0;


  ///开启计时器
  void startCountdownTimer() {
    _timer = Timer.periodic(
        Duration(seconds: (widget.periodicSec)??1),
            (Timer timer) => {
          setState(() {
            if (_countdownTime == 0) {
              if(widget.onTimerFinish!=null){
                widget.onTimerFinish!();
              }
              _timer?.cancel();
            } else {
              _countdownTime--;
              if(widget.onPeriodic!=null){
                widget.onPeriodic!();
              }
            }
          })
        });
  }

  ///根据当前状态返回文本对应样式
  TextStyle? checkStyle(){
    if(_countdownTime==0){
      if(widget.themeColor!=null) {
        return TextStyle(fontSize: 14,color: widget.themeColor);
      } else {
        return TextStyle(
          fontSize: 12,
          fontWeight: FontWeight.w400,
          color: XinHeColor("#FF006A"),
        );
      }
    }else{
      return TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.w400,
        color: XinHeColor("#BCC7D4"),
      );
    }
  }

  Color? checkStyleColor(){
    if(_countdownTime==0){
      if(widget.themeColor!=null) {
        return widget.themeColor;
      } else {
        return XinHeColor('#FFB762');
      }
    }else{
      return XinHeColor("#BCC7D4");
    }
  }

  @override
  void dispose() {
    if (_timer != null) {
      _timer?.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () async {
          if (_countdownTime == 0) {
            //开始倒计时
            if(widget.onStarEvent!=null){
              if(!(await widget.onStarEvent!())) return;
            }
            _countdownTime = widget.maxSec??60;
            startCountdownTimer();
          }
        },
        child: Container(
          alignment: Alignment.center,
          height: 32,
          width: 90,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(16),
              // border: Border.all(color: checkStyleColor()!,width: 1)
          ),
          child: Text(
            _countdownTime==0?widget.norText??'获取验证码':'${_countdownTime}s',
            style: checkStyle(),
          ),
        )


    );
  }

}
